//通过vue-router实现模板路由配置
import { createRouter, createWebHashHistory } from "vue-router";
import { constantRoute } from "./routes";
import useUserStore from "../store/modules/user";
import nprogress from "nprogress";
import "nprogress/nprogress.css";
nprogress.configure({ showSpinner: false });

let router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoute,
  //滚动行为
  scrollBehavior() {
    return {
      left: 0,
      top: 0,
    };
  },
});

router.beforeEach((to, from, next) => {
  nprogress.start();
  const token = useUserStore().token;
  const routes = ["/home", "/search", "/person", "/login", "/detail"];
  if (to.path == "/login") next();
  if (token) {
    next();
  } else {
    if (to.path == "/login") {
      next();
    } else {
      next({ path: "login", query: { redirect: to.path } });
    }
  }
});

//全局后置守卫
router.afterEach((to, from, next) => {
  nprogress.done();
});

export default router;
